package service

import (
	"forum/pkg/domain"

	"github.com/go-playground/validator/v10"
)

type profileService struct {
	profileRepo domain.ProfileReposiroty
}

func NewProfileService(profileRepo domain.ProfileReposiroty) domain.ProfileService {
	return &profileService{
		profileRepo: profileRepo,
	}
}

func (svc *profileService) CheckProfile(profile domain.Profile) error {
	validate := validator.New()

	return validate.Struct(&profile)
}

func (svc *profileService) UpdateProfile(profile *domain.Profile) error {

	return svc.UpdateProfile(profile)
}

func (svc *profileService) GetProfileByUserID(userId string) (*domain.Profile, error) {
	profile, err := svc.profileRepo.GetProfileByUserID(userId)
	if err != nil {
		user, err := svc.profileRepo.GetUserByID(userId)
		if err != nil {
			return nil, err
		}
		profile := &domain.Profile{
			UserID:   user.UUID,
			Username: user.Name,
			Email:    user.Email,
		}
		return profile, nil
	}

	return profile, nil
}

func (svc *profileService) GetUserByID(userID string) (*domain.User, error) {
	return svc.profileRepo.GetUserByID(userID)
}

func (svc *profileService) GetProfileByName(username string) (*domain.Profile, error) {
	return svc.profileRepo.GetProfileByName(username)
}
